
package tfoundation.feed;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import tfoundation.Body;
import tfoundation.world.World;

public class DisplayFeed
{
	private static final int SIZE_VALUE = 16;

	private final World world;

	private final List<BodyDisplay> bodyList;

	public int worldCenterX = 0;
	public int worldCenterY = 0;

	public DisplayFeed(World world)
	{
		this.world = world;

		this.bodyList = new ArrayList<BodyDisplay>();
	}

	public synchronized ListIterator<BodyDisplay> getBodies()
	{
		return this.bodyList.listIterator();
	}

	public void readWorld(Body hero)
	{
		final int heroX = hero.getLocationX();
		final int heroY = hero.getLocationY();

		ListIterator<Body> worldIterator = null;
		Body temp = null;
		BodyDisplay bodyDisplay = null;
		int tempSize1 = 0;
		int tempSize2 = 0;
		this.bodyList.clear();

		worldIterator = this.world.getBodies().listIterator();
		while (worldIterator.hasNext())
		{
			temp = worldIterator.next();
			if (temp != hero)
			{
				tempSize1 = DisplayFeed.SIZE_VALUE * temp.getSizeClass();
				tempSize2 = tempSize1 / 2;

				bodyDisplay = new BodyDisplay(temp);

				bodyDisplay.x = temp.getLocationX() - heroX;
				bodyDisplay.y = temp.getLocationY() - heroY;

				bodyDisplay.left = bodyDisplay.x + tempSize2;
				bodyDisplay.right = bodyDisplay.x - tempSize2;
				bodyDisplay.top = bodyDisplay.y + tempSize2;
				bodyDisplay.bottom = bodyDisplay.y - tempSize2;

				bodyDisplay.distance = hero.getDistance(temp);

				bodyDisplay.height = tempSize1;
				bodyDisplay.width = tempSize1;

				this.bodyList.add(bodyDisplay);
			}
		}
	}
}
